package com.momochen.alarmclockdemo.activity;

import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.momochen.alarmclockdemo.AlarmConstant;
import com.momochen.alarmclockdemo.AlarmControl;
import com.momochen.alarmclockdemo.R;
import com.momochen.alarmclockdemo.bean.AlarmBean;
import com.momochen.alarmclockdemo.db.AlarmBeanDaoHelper;

import java.util.Calendar;

/**
 * Created by momochen on 2017/7/9.
 */

public class EditActivity extends FragmentActivity implements View.OnClickListener, TimePickerDialog.OnTimeSetListener {

    TextView tvTime;
    Button btnSave;
    Button btnDel;

    AlarmBean alarmBean;

    int mHour;
    int mMinutes;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);

        tvTime = this.findViewById(R.id.tv_time);
        tvTime.setOnClickListener(this);
        btnDel = this.findViewById(R.id.btn_del);
        btnDel.setOnClickListener(this);
        btnSave = this.findViewById(R.id.btn_save);
        btnSave.setOnClickListener(this);

        alarmBean = (AlarmBean) getIntent().getSerializableExtra("ALARM_BEAN");

        initViews();
    }

    private void initViews() {
        if (alarmBean != null) {
            mHour = alarmBean.getHour();
            mMinutes = alarmBean.getMinutes();

            final Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, alarmBean.getHour());
            calendar.set(Calendar.MINUTE, alarmBean.getMinutes());
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            refreshTime(calendar.getTimeInMillis());
        } else {
            final Calendar calendar = Calendar.getInstance();

            mHour = calendar.get(Calendar.HOUR_OF_DAY);
            mMinutes = calendar.get(Calendar.MINUTE);
            refreshTime(calendar.getTimeInMillis());
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_time:
//                new TimePickerDialog(this, this, 0, 0, DateFormat.is24HourFormat(this)).show();
                new TimePickerDialog(this, this, 0, 0, true).show();
                break;
            case R.id.btn_del:
                delAlarm();
                break;
            case R.id.btn_save:
                saveAlarm();
                break;
        }
    }

    private void refreshTime(long time) {
        tvTime.setText(DateFormat.format(AlarmConstant.DM24, time));
    }

    @Override
    public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
        mHour = hourOfDay;
        mMinutes = minute;

        final Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        refreshTime(calendar.getTimeInMillis());
    }

    private void saveAlarm() {
        if (alarmBean == null) {
            alarmBean = new AlarmBean();
            alarmBean.setEnabled(true);
            alarmBean.setHour(mHour);
            alarmBean.setMinutes(mMinutes);
            alarmBean.setDaysOfWeek(0);
            alarmBean.setIsVibrate(true);
            alarmBean.setNote("坐飞机");
            alarmBean.setIsSilent(true);


            alarmBean.setId(AlarmBeanDaoHelper.saveOrUpdateAlarm(alarmBean));
        } else {
            alarmBean.setHour(mHour);
            alarmBean.setMinutes(mMinutes);

            AlarmBeanDaoHelper.saveOrUpdateAlarm(alarmBean);
        }

        AlarmControl.addAlarm(this, alarmBean);

//        finish();
    }

    private void delAlarm() {
        if (alarmBean != null) {
            AlarmBeanDaoHelper.deleAlarm(alarmBean.getId());
            finish();
        } else {
            Toast.makeText(this, "删除失败", Toast.LENGTH_LONG).show();
        }

    }
}
